# _*_ coding:utf-8 _*_
# @Time  : 2022.10.19
# @Author: zizlee
import json
import pathlib
import random
import time

import requests
import datetime
import pandas as pd
from urllib3 import disable_warnings
disable_warnings()

HOST = 'https://210.13.218.130:9000/v1/'

USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " \
             "Chrome/86.0.4240.198 Safari/537.36"

VARIETY = {
    '豆一': 'A',
    '豆二': 'B',
    '玉米': 'C',
    '玉米淀粉': 'CS',
    '苯乙烯': 'EB',
    '乙二醇': 'EG',
    '纤维板': 'FB',
    '铁矿石': 'I',
    '焦炭': 'J',
    '鸡蛋': 'JD',
    '焦煤': 'JM',
    '聚乙烯': 'L',
    '生猪': 'LH',
    '豆粕': 'M',
    '棕榈油': 'P',
    '液化石油气': 'PG',
    '聚丙烯': 'PP',
    '粳米': 'RR',
    '聚氯乙烯': 'V',
    '豆油': 'Y',
    '胶合板': 'BB'
}


def get_variety_en(vn):
    en = VARIETY.get(vn)
    if not en:
        raise ValueError('{}的品种代码不存在!'.format(vn))
    return en


# 获取月份的交割数据文件
def get_delivery_file(bm, em):
    headers = {'User-Agent': USER_AGENT,
               'Host': 'www.dce.com.cn'}
    form_params = {
        'deliveryQuotes.variety': 'all',
        'year': '',
        'month': '',
        'deliveryQuotes.begin_month': bm,  # 202201
        'deliveryQuotes.end_month': em
    }
    r = requests.post('http://www.dce.com.cn/publicweb/quotesdata/delivery.html', headers=headers, data=form_params)
    with open('dce/delivery/{}.html'.format(bm), 'wb') as fp:
        fp.write(r.content)
    print('获取{}.DCE交割数据文件完成！'.format(bm))


# 解析月交割数据文件
def parse_delivery_file(month, delivery_date):
    df = pd.read_html('dce/delivery/{}.html'.format(month), encoding='utf8')[0]
    # 取品种含有小计的
    df = df[df['品种'].str.contains('小计')]
    df['品种'] = df['品种'].apply(lambda x: x.replace('小计', ''))
    df['variety_en'] = df['品种'].apply(lambda x: get_variety_en(x))
    df.rename(columns={'品种': 'variety_name', '交割量': 'volume', '交割金额': 'amount'}, inplace=True)
    df = df[['variety_name', 'variety_en', 'volume', 'amount']]
    df['delivery_date'] = delivery_date
    df['volume_unit'] = '手'
    df['amount_unit'] = '元'
    df_data = df.to_dict(orient='records')
    print('----- 解析{}.DCE交割文件成功,数量:{} -----'.format(delivery_date, len(df_data)))
    if len(df_data) > 0:
        folder = pathlib.Path("resolution/{}/".format(delivery_date))
        if not folder.exists():
            folder.mkdir(parents=True)
        resolution_delivery_file = pathlib.Path("resolution/{}/DCE_Delivery.json".format(delivery_date))
        # 保存到本地文件
        with open(resolution_delivery_file, 'w', encoding='utf8') as fp:
            json.dump(df_data, fp, indent=2, ensure_ascii=False)


def save_delivery_to_server(delivery_date: datetime.date):
    quote_date_string = delivery_date.strftime('%Y%m%d')
    resolution_delivery_file = pathlib.Path("resolution/{}/DCE_Delivery.json".format(quote_date_string))
    if not resolution_delivery_file.exists():
        raise ValueError('{}.DCE行情数据文件不存在,请先解析保存！'.format(quote_date_string))
    # 读取本地文件
    with open(resolution_delivery_file, 'r', encoding='utf8') as fp:
        json_data = json.load(fp)
    # 保存到目标API
    r = requests.post(url=HOST + 'exchange/delivery/', json=json_data, verify=False)
    print(r.json())


if __name__ == '__main__':
    start = datetime.date(2023, 11, 1)  # 201301开始
    end = datetime.date(2023, 12, 1)
    business_days_rng = pd.date_range(start, end, freq='BM')
    not_delivery_dates = [datetime.date(2015, 4, 30)]
    for d in business_days_rng:
        month = d.strftime('%Y%m')
        month_last = d.strftime('%Y%m%d')
        if d in not_delivery_dates:
            continue
        print(month, month_last)
        get_delivery_file(bm=month, em=month)  # 获取月份的交割数据文件
        parse_delivery_file(month, month_last)  # 解析月份的交割数据文件
        save_delivery_to_server(delivery_date=d)  # 将交割数据保存到服务器
        time.sleep(1)
